package com.jt.controller;

import com.jt.pojo.Role;
import com.jt.service.RightsService;
import com.jt.service.RoleService;
import com.jt.vo.RoleRightsVO;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘昱江
 * 时间 2021/3/3
 */
@RestController
@RequestMapping("/role")
//@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 根据角色获取所有权限
     */
    @GetMapping("/getRoleList")
    public SysResult findRoleRightsList(){

        List<Role> roleList = roleService.findRoleRightsList();
        return SysResult.success(roleList);
    }

    /**
     * 删除角色对应的权限
     * URL: http://localhost:8091/role/deleteRoleRights?roleId=1&id=14
     * 参数: roleId/rightsId
     * 返回值: SysResult对象
     */
    @DeleteMapping("deleteRoleRights")
    public SysResult deleteRoleRights(Integer roleId,Integer rightsId,Integer level){
        //要求删除完成之后,返回最新的权限列表
        Role role = roleService.deleteRoleRights(roleId,rightsId,level);
        return SysResult.success(role);
    }

    //根据roleId 查询已经分片的rightsId
    @GetMapping("getRoleRightsList")
    public SysResult getRoleRightsList(Integer roleId){
        //要求删除完成之后,返回最新的权限列表
        List<Integer> rightsList = roleService.findRightsIdByOrderId(roleId);
        return SysResult.success(rightsList);
    }

    /**
     * 根据roleId保存权限数据
     * URL地址:http://localhost:8091/role/submitRights/1
     * 参数: rightsIds,roleId
     * 返回值: SysResult对象
     */
    @PostMapping("/submitRights")
    public SysResult saveRights(Integer roleId,Integer... rightsIds){

        roleService.saveRights(roleId,rightsIds);
        return SysResult.success();
    }
}
